package com.example.impl;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SolrSerivceImpl {

    @Autowired
    private SolrClient solrClient;

    public SolrDocumentList listGoods(String key){

        SolrDocumentList results = null;
        try {
            SolrQuery solrQuery = new SolrQuery("goodName:" + key);
            //设置查询的条数用于分页
            solrQuery.setRows(1);
            //设置查询的开始用于分页
            solrQuery.setStart(0);

            //设置高亮
            //solrQuery.setHighlight(true);
            //solrQuery.addHighlightField("goodsName");
            //solrQuery.setHighlightSimplePre("<label style='yellow'>");
            //solrQuery.setHighlightSimplePost("</label>");
            //执行查询
            QueryResponse queryResponse = solrClient.query(solrQuery);
            //返回高亮显示结果
            //Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();

            //遍历查询结果（每个SolrDocument对象就表示一行查询记录）
            results = queryResponse.getResults();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return results;
    }
}
